//
//  BBHelpViewController.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/11/9.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class BBHelpViewController: BBBaseViewController {
    private var helpCellDic = ["Repair": "help_Repair", "Suggestion": "help_Suggestion", "Question": "help_Question","Nearby":"location"]
    override func viewDidLoad() {
        super.viewDidLoad()

        // 1.如果没有登录, 就设置未登录界面的信息
        if !userLogin {
            visitorView?.setupVisitorInfo(animationType: 4, imageName: "Visitor_help_back", message: "Quick access to after-sales service.")
            return
        }
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "WorkOrder", style: UIBarButtonItem.Style.plain, target: self, action: #selector(workOrderAction))
        createHelpCellView()
    }
    
    @objc private func workOrderAction(){
        let vc = BBHelpListViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }

    private func createHelpCellView() {
        
        for dic in helpCellDic {
            if dic.key == "Question"{
                let iv = HelpCell(frame: CGRect(x: 10, y: 100, width: UIScreen.main.bounds.size.width - 20, height: 50))
                iv.backgroundColor = UIColor(red:  245.0 / 255.0, green:   245.0 / 255.0, blue: 245.0 / 255.0, alpha: 1)//UIColor(red: 249.0 / 255.0, green: 245.0 / 255.0, blue: 237.0 / 255.0, alpha: 1)
                iv.titleLabel.text = dic.key
                iv.leftImage.image = UIImage(named: dic.value)
                iv.touchBT.tag = 1
                iv.touchBT.addTarget(self, action: #selector(touchBTAction(sender:)), for: UIControl.Event.touchUpInside)
                view.addSubview(iv)
            }else  if dic.key == "Suggestion"{
                let iv = HelpCell(frame: CGRect(x: 10, y: 160, width: UIScreen.main.bounds.size.width - 20, height: 50))
                iv.backgroundColor = UIColor(red:  245.0 / 255.0, green:   245.0 / 255.0, blue: 245.0 / 255.0, alpha: 1)//UIColor(red: 249.0 / 255.0, green: 245.0 / 255.0, blue: 237.0 / 255.0, alpha: 1)
                iv.titleLabel.text = dic.key
                iv.leftImage.image = UIImage(named: dic.value)
                iv.touchBT.tag = 2
                iv.touchBT.addTarget(self, action: #selector(touchBTAction(sender:)), for: UIControl.Event.touchUpInside)
                view.addSubview(iv)
            }
            else  if dic.key == "Repair"{
                let iv = HelpCell(frame: CGRect(x: 10, y: 220, width: UIScreen.main.bounds.size.width - 20, height: 50))
                iv.backgroundColor = UIColor(red:  245.0 / 255.0, green:   245.0 / 255.0, blue: 245.0 / 255.0, alpha: 1)//UIColor(red: 249.0 / 255.0, green: 245.0 / 255.0, blue: 237.0 / 255.0, alpha: 1)
                iv.titleLabel.text = dic.key
                iv.leftImage.image = UIImage(named: dic.value)
                iv.touchBT.tag = 3
                iv.touchBT.addTarget(self, action: #selector(touchBTAction(sender:)), for: UIControl.Event.touchUpInside)
                view.addSubview(iv)
            }else  if dic.key == "Nearby"{
                let iv = HelpCell(frame: CGRect(x: 10, y: 280, width: UIScreen.main.bounds.size.width - 20, height: 50))
                iv.backgroundColor = UIColor(red:  245.0 / 255.0, green:   245.0 / 255.0, blue: 245.0 / 255.0, alpha: 1)
                iv.titleLabel.text = dic.key
                iv.leftImage.image = UIImage(named: dic.value)
                iv.touchBT.tag = 4
                iv.touchBT.addTarget(self, action: #selector(touchBTAction(sender:)), for: UIControl.Event.touchUpInside)
                view.addSubview(iv)
            }
        }
    }
    
    @objc private func touchBTAction(sender:UIButton){
        switch sender.tag {
        case 1:
            let vc = BBQuestionViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            break
        case 2:
            let vc = BBSuggestionViewController.init(nibName: "BBSuggestionViewController", bundle: nil)
            self.navigationController!.pushViewController(vc, animated: true)
            break
        case 3:
            let vc = BBRepairOneViewController.init(nibName: "BBRepairOneViewController", bundle: nil)
            self.navigationController!.pushViewController(vc, animated: true)
            break
        default:
            print("4")
        }
    }
}

class HelpCell: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(leftImage)
        addSubview(titleLabel)
        addSubview(rightImage)
        addSubview(touchBT)

        leftImage.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: self, size: CGSize(width: bounds.size.height - 15, height: bounds.size.height - 15), offset: CGPoint(x: 5, y: 8))
        titleLabel.xmg_AlignVertical(type: XMG_AlignType.CenterRight, referView: leftImage, size: CGSize(width: 120, height: bounds.size.height - 10), offset: CGPoint(x: 10, y: 0))
        rightImage.xmg_AlignInner(type: XMG_AlignType.CenterRight, referView: self, size: CGSize(width: 25, height: bounds.size.height - 20), offset: CGPoint(x: 0, y: 0))
        touchBT.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: self, size: CGSize(width: bounds.size.width, height: bounds.size.height))
    }

    public lazy var leftImage: UIImageView = {
        let iv = UIImageView()
        return iv
    }()

    public lazy var titleLabel: UILabel = {
        let la = UILabel()
        la.font = UIFont.systemFont(ofSize: 18)
        la.textColor = UIColor(red: 60 / 255.0, green: 60 / 255.0, blue: 60 / 255.0, alpha: 1)
        return la
    }()

    private lazy var rightImage: UIImageView = {
        let iv = UIImageView()
        iv.image = UIImage(named: "help_right")
        return iv
    }()

    public lazy var touchBT: UIButton = {
        let bt = UIButton()
        return bt
    }()

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
